#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main()
{
  // 系统接口
  umask(0);
  //int fp = open("test.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);  
  int fp = open("test.txt", O_RDWR); 
  printf("fp: %d, errno: %d, errstring: %s\n", fp, errno, strerror(errno));
 
  
  char buff[1024];
  ssize_t n = read(fp, buff, sizeof(buff) - 1);
  if(n > 0)
  {
    buff[n] = '\0';

    printf("%s\n", buff);
  }
  //int cnt = 1;
  //const char* msg = "???sfaasdf?????";
  //while(cnt)
  //{
  //  char buff[128];
  //  snprintf(buff, sizeof(buff), "fp: %d, msg: %s, cnt: %d\n", fp, msg, cnt);

  //  write(fp, buff, strlen(buff));
  //  --cnt;
  //}

  close(fp);
  
  // ------------------------------------------------------
  // C文件接口
  //FILE* fp = fopen("test.txt", "w");
  //FILE* fp = fopen("test.txt", "w");
  //FILE* fp = fopen("test.txt", "a");
  //if(fp == NULL)
  //{
  //  perror("fopen");
  //  exit(-1);
  //}
  // 读取 
  //while(1)
  //{
  //  char buf[128];
  //  if((fgets(buf, sizeof(buf), fp) == NULL)) break;
  //  else printf("%s", buf);
  //}
  // 写入
  //const char* msg = "hello world\n";
  //int n = 5;
  //while(n)
  //{
  //  fputs(msg, fp);
  //  --n;
  //}
  

  //const char* msg = "hello world";
  //int n = 5;
  //while(n)
  //{
  //  //fprintf(fp, "%s, %d, xkl\n", msg, n);
  //  char buffer[128];// 自定义缓冲区
  //  snprintf(buffer, sizeof(buffer), "%s, %d, xkl\n", msg, n);

  //  fputs(buffer, fp);
  //  --n;
  //} 
  //fclose(fp);
  return 0;
}
